<?php
namespace test;

use test\primary\sql_action;
use test\primary\set_cookie;
use test\primary\set_session;

include 'primary\sql_action.php';
include 'primary\set_cookie.php';
include 'primary\set_session.php';

$set_cookie = new set_cookie();
$set_session = new set_session();

$username = $_POST['username'];
$pwd = $_POST['pwd'];
$time=time();
$md5 = md5($username . $pwd . $time);
if ($username == '' || $pwd == '') {
    $win = 1;
}
$conn = new \mysqli();
$conn->connect('127.0.0.1', 'root', '123456', 'userinfo');
if (!empty($conn->connect_error)) {
    $win = 2;
}

sql_action::set_conn($conn);
$sql = ('select * from user_info where Account=\'' . $username . '\' and pwd=\'' . $pwd . '\'');

$login_test = sql_action::select_test($sql);
if ($login_test) {
    $win = 0;
} else {
    $win = 3;
}
if ($login_test) {
    $set_cookie->set_cookie($md5, $username, $pwd,$time+100*60);
    $set_session->set_session($md5, $username, $pwd,$time+100*60);
}


?>
<!DOCTYPE html>
<script>
    switch (<?php echo $win ?>) {
        case 1:
            alert('账号或密码为空');
            window.location.href = 'login.php';
            break;
        case 2:
            alert('数据库连接失败，请联系管理员');
            window.location.href = 'login.php';
            break;
        case 3:
            alert('账号或密码错误');
            location.href = 'login.php';
            break;
        case 0:
            alert('登陆成功！');
            window.location.href = 'user_function.php';
            break;
    }
</script>
